package org.dromara.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.domain.AuditDto;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.erp.domain.ErpCostOut;
import org.dromara.erp.domain.ErpCostOutInfo;
import org.dromara.erp.mapper.ErpCostOutInfoMapper;
import org.dromara.erp.mapper.ErpCostOutMapper;
import org.dromara.erp.service.IErpCostOutService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 费用支出Service业务层处理
 *
 * @author Mr Lei QQ:235921049
 * @date 2024-06-22
 */
@RequiredArgsConstructor
@Service
public class ErpCostOutServiceImpl implements IErpCostOutService {

    private final ErpCostOutMapper baseMapper;
    private final ErpCostOutInfoMapper infoMapper;

    /**
     * 查询费用支出
     */
    @Override
    public ErpCostOut queryById(Long id){
        //查询主表ID
        ErpCostOut costOut = baseMapper.selectById(id);
        //创建wrapper对象
        LambdaQueryWrapper<ErpCostOutInfo> infoWrapper = new LambdaQueryWrapper<>();
        //子表订单ID等于主表ID
        infoWrapper.eq(ErpCostOutInfo::getOrderId,id);
        //查询符合条件的列表
        List<ErpCostOutInfo> infos = infoMapper.selectList(infoWrapper);
        //设置主表
        costOut.setContentInfo(infos);
        //返回
        return costOut;
    }

    /**
     * 查询费用支出列表
     */
    @Override
    public TableDataInfo<ErpCostOut> queryPageList(ErpCostOut bo, PageQuery pageQuery) {
        Page<ErpCostOut> result = baseMapper.queryPageList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询费用支出列表
     */
    @Override
    public List<ErpCostOut> queryList(ErpCostOut bo) {
        LambdaQueryWrapper<ErpCostOut> lqw = buildQueryWrapper(bo);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<ErpCostOut> buildQueryWrapper(ErpCostOut bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpCostOut> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getOrderNo()), ErpCostOut::getOrderNo, bo.getOrderNo());
        lqw.eq(bo.getWorkerId() != null, ErpCostOut::getWorkerId, bo.getWorkerId());
        lqw.eq(bo.getDeptId() != null, ErpCostOut::getDeptId, bo.getDeptId());
        lqw.eq(bo.getAuditStatus() != null, ErpCostOut::getAuditStatus, bo.getAuditStatus());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            ErpCostOut::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        return lqw;
    }

    /**
     * 新增费用支出
     */
    @Override
    public Boolean insertByBo(ErpCostOut bo) {
        validEntityBeforeSave(bo);
        boolean flag = baseMapper.insert(bo) > 0;
        if (flag) {
            //增加成功后返回ID
            bo.setId(bo.getId());
            //查询返回的子表
            List<ErpCostOutInfo> contentInfo = bo.getContentInfo();
            //遍历
            contentInfo.forEach(x->{
                x.setOrderId(bo.getId());
                //执行子表新增方法
                infoMapper.insert(x);
            });
        }
        return flag;
    }

    /**
     * 修改费用支出
     */
    @Override
    public Boolean updateByBo(ErpCostOut bo) {
        validEntityBeforeSave(bo);
        //查询子表
        List<ErpCostOutInfo> contentInfo = bo.getContentInfo();
        contentInfo.forEach(x->{
            x.setOrderId(bo.getId());
            //如果子表有新增
            if(x.getId() ==null){
                infoMapper.insert(x);
            }
            //执行子表修改方法
            infoMapper.updateById(x);
        });
        return baseMapper.updateById(bo) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpCostOut entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除费用支出
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
    /*默认标题*/
    @Override
    public String defaultTitle(String formatDate) {
        Integer totalNum = baseMapper.selectByDateCunt(formatDate);
        //最低3位数，不足前面用0填充
        //拼接成新的标题
        return String.format("%03d", totalNum);
    }
    /*审核操作*/
    @Override
    public void updateAudit(AuditDto dto) {
        LambdaUpdateWrapper<ErpCostOut> updateWrapper = new LambdaUpdateWrapper<>();
        //设置更新条件
        updateWrapper.eq(ErpCostOut::getId,dto.getId());
        //更新字段
        updateWrapper.set(ErpCostOut::getAuditStatus,dto.getAuditStatus());
        updateWrapper.set(ErpCostOut::getAuditRemark,dto.getAuditRemark());
        updateWrapper.set(ErpCostOut::getAuditTime,new Date());
    }
}
